# -*- coding: utf-8 -*-

import urllib
import re
import logging
import urllib2

import xml.etree.ElementTree as ElementTree

import html5lib
from html5lib import treebuilders, treewalkers, serializer

from onlinebrary.lib.aggregator.abstract_aggregator import AbstractAggregator
from onlinebrary.model.agg_data import AggregatedData

log = logging.getLogger(__name__)

SEARCH_URL = 'http://ntest.fictionbook.ru/search?'
KEYWORDS_PARAM = 'search'
#PAGE_PARAM = 'page'
CONTENT_ENCODING = 'utf-8'
REQUEST_ENCODING = 'utf-8'

class NewFictionbookRuAggregator(AbstractAggregator):
    
    def __init__(self):
        self.name = "New Fictionbook.ru"

    def search_by_title(self, title):
        
        log.info('Searching %s in %s', title, self.name)
        
        data = AggregatedData(self.name)
    
        title_encoded = title.encode(REQUEST_ENCODING)
        try:
            query = urllib.urlencode(((KEYWORDS_PARAM, title_encoded),))
        except Exception, e:
            log.error(e)
            return e
            
        url = SEARCH_URL + query
        log.info('Requesting search URL: %s', url)

        data.source_search = url

        p = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("etree", ElementTree))
        tree = p.parse(urllib2.urlopen(url).read())

        results = dict()
        results_count = 0
        serie_name = ""
        for div in tree.findall(r'.//div'):
            if div.attrib.has_key('class') and div.attrib['class'] == 'serie':
                serie_name = div.find('h3').text
                if div.find('h3/small'):
                    serie_name += (" <small>%s</small>" % div.find('h3/small').text)
                results[serie_name] = []
            if div.attrib.has_key('class') and div.attrib['class'] == 'b_t_l':
                link_element = div.find('strong/a')
                link = link_element.attrib['href']
                title = link_element.text
                results[serie_name].append("<a href='http://ntest.fictionbook.ru%s' target='_blank'>%s</a>" % (link, title))
                results_count += 1

        data.count = results_count
        data.data = results
        
        return data
